/**
 * 
 */

 var cfgGrids={
	    		provincecfg:{
	    			title:"企业号管理",
	    			cols: {
	    				corpId:{name:"企业号CORPID",type:"string",show:true},
	    				province:{name:"名称标识",type:"string",show:true},// 只能采用隐藏的方式
	    				provincename:{name:"企业号描述",type:"string",show:true,width:"5%"},
	    				corpSecret:{name:"CORPSECRET",type:"string",show:true,width:"20%"},
	    				aesKey:{name:"AESKEY",type:"string",show:true,width:"20%"},
	    				used:{name:"启用",type:"bool",show:true,width:"3%"},
	    				url:{name:"后台URL",type:"string",show:true},
	    				corpicon:{name:"企业号图标",type:"string",show:true}
	    				//,
	    				//extra:{name:"应用",show:true,width:"5%",content:"<span class='extra' style='width:5%;'><i  class='fa fa-cubes fa-lg'></i></span>"}//扩展编辑字段并不存入数据库
	    			}
	    		}
  };
	    

  $(function($){

		     adjustwindow();
		   	 function   adjustwindow(){
		     	$(".dgride .content").height($(".dgride").height()-$(".dgride .title").height()-5);
		    	$(".dgride").width($(".ds_view").width());
		    	
		   	 } 
		    
		    
		
 
 	});
  function adjustScroll(){
	  
	//==============
  	var tar=$(".dgride .scroll")[0];
		 if(tar.style.overflow!="hidden"&&tar.scroll!="no"&&
				 tar.scrollHeight>tar.offsetHeight)
				{
				 alert("有滚动条");
				}else 
				{ 
				 alert("无滚动条");
				}
  }
	    
	  
	    
	    $.fn.dgrid =function(options){
	    	 
	    	  var Grid = function(ele, opt) {
	    	    this.gd=ele;;
	    	    var gbody=  $("<div class='gbody'></div>").appendTo(ele);
	    	    this.header=  $("<div class='gbheader gradient'></div>").appendTo(gbody);
	    	    var scroll=$("<div class='scroll'></div>").appendTo(gbody);
	    	    this.rows=     $("<ul class='items'></ul>").appendTo(scroll);
//	  	        this.rows= ele.find(".items");
//	  	        this.header=ele.find(".gbheader");
	  	        
	  	        
	  	        this.defaults = { 
	  	        		gdcfg:cfgGrids.provincecfg,geturl:CONETXT_PATH+"" 
	  	        };
	  	        this.options = $.extend({}, this.defaults, opt);
	  	      
	  	     
	  	      //注册基本事件
		        $("body").on("click",  "#"+this.gd.attr("id")+"  .items .item" ,function(){
			    	 var tar=$(this);
			    	 tar.siblings().removeClass("active");
			    	if( tar.hasClass("active")){
			    		tar.removeClass("active");
			    	}else{
			    		tar.addClass("active");
			    	}
			    });
	 
				
	    	  };
	    	  
	    	  
	  	     //根据数据生成行数据
	    	  var addRowItem=function( data,cols){
	    			 
				    	var row="<li class='item' readonly=readonly>"
					 	$.each(data,function(key,value){
					 		var type ='text';
					 		var width=cols[key].width;
					 		var widthstyle="style='width: "+width+";'"

                              if(!cols[key].show) return;
					 		  if(cols[key].type=="bool")
					 			{
					 			   type="checkbox";
					 			   var checked=(value==true)?"checked":"";
					 			   row+= "<input class='"+key+"' type='"+type+"' "+widthstyle+"  "+checked+"    onclick='return false'>";
					 			}else
						 		 {
						 				row+= "<input class='"+key+"' type='  "+type+"' value='"+value+"' "+widthstyle+" readonly   >";
						 		 }
					 	});
				    	
					 	row+= "<span  class='colTool'> <a title='同步应用'  class='synAgent  fa fa-cubes fa-lg' > </a>  <a title='编辑' class='edit  fa fa-edit fa-lg '>  </a><a title='同步部门人员' class='syncDepart fa fa-recycle fa-lg'>   </a><a title='刷新菜单' class='syncMenu fa fa-list-alt fa-lg'>   </a> </span> </li>"
					 	return row;
			  };
	    	//定义Beautifier的方法
	    	  Grid.prototype = {
	    			     /**
					                      添加新行
						 */
						 addRowItem: function(data){
							 var tar= this.rows;
							 var options=this.options;
							 tar.append(addRowItem(data,options.gdcfg.cols));
							 },
						 init:function(){
						      var gdrows=  this.rows;
						      var options=this.options;
						      var header=this.header;
						
							 //初始化 
				              
					          $.getJSON(options.geturl, function(json){
						      		// 使用模版
						         	var rows="";
						  	        $.each(json, function(index,rowdata) {
						  	        	rows+= addRowItem(rowdata,options.gdcfg.cols);
						  	        });
						  	  
						  	      gdrows.html(rows);
						  	     initheader(header);
						     
						     });
							 
					          //初始化 header
							 function initheader(gdheader){
 
					       
							 var row="";
							 
							 $.each(options.gdcfg.cols,function(self,item){
								 if(item.show) 
								    gdheader.append("<span class='"+self+"'>"+item.name+"</span>") ;
								// row+="<span class='"+self+"'>"+item.name+"</span>";
								 if(item.width)
								 $("."+self).width(item.width);
							 });
							 }
							// gdheader.html(row);
							 
						 } 
					 
	    	  }
	     //新建表格类	
	     var gd =new Grid(this, options);
	     gd.init();//初始化表格
         return gd ;  
      };
      

	  $.extend({
		    log: function(message) {
		        var now = new Date(),
		            y = now.getFullYear(),
		            m = now.getMonth() + 1, //！JavaScript中月分是从0开始的
		            d = now.getDate(),
		            h = now.getHours(),
		            min = now.getMinutes(),
		            s = now.getSeconds(),
		            time = y + '/' + m + '/' + d + ' ' + h + ':' + min + ':' + s;
		        console.log(time + ' My App: ' + message);
		    }
	 });
	  
   //$.log('initializing...'); //调用  